varying vec3 lightVec, lightVecTan;
varying vec3 halfVec, halfVecTan;
varying vec2 texCoord;
varying vec3 shadowCoord;
varying vec3 normal;
attribute vec3 tangent;
uniform int texOn, bumpOn, shadowOn;
uniform int shadowMapNum;

void calcShadowCoord()
{
	vec4 realPos = gl_ModelViewMatrix * gl_Vertex;
	realPos = gl_TextureMatrix[shadowMapNum] * realPos;
	shadowCoord = realPos.xyz / realPos.w;	
}

void calcTexCoord()
{
	texCoord = gl_MultiTexCoord0.xy;	
}

void calcTangentSpace(const vec3 n, const vec3 lightVec, const vec3 halfVec)
{
	vec3 t = normalize(gl_NormalMatrix * tangent);
	vec3 b = cross(n, t);
	
	lightVecTan.x = dot(lightVec, t);
	lightVecTan.y = dot(lightVec, b);
	lightVecTan.z = dot(lightVec, n);
	
	halfVecTan.x = dot(halfVec, t);
	halfVecTan.y = dot(halfVec, b);
	halfVecTan.z = dot(halfVec, n);	
}

void main(void)
{
	if(1 == shadowOn)
		calcShadowCoord();
	
	if(1 == texOn)
		calcTexCoord();		
		
	normal = normalize(gl_NormalMatrix * gl_Normal);	
	lightVec = normalize(vec3(gl_LightSource[0].position));	
	halfVec = normalize(gl_LightSource[0].halfVector.xyz);
	
	if(1 == bumpOn)
		calcTangentSpace(normal, lightVec, halfVec);
		
	gl_FrontColor = gl_Color;
	
	gl_ClipVertex = gl_ModelViewMatrix * gl_Vertex;
	
	gl_Position = ftransform();
}